<?php

namespace app\common\payplug;

use think\facade\Env;
use EasyWeChat\Factory;

class WechatPay
{
    /**
     * app支付
     *
     * @param [type] $body  商品描述
     * @param [type] $out_trade_no  订单编号
     * @param [type] $total_fee 订单金额
     * @param [type] $notify_url    回调地址
     * @return void
     */
    public function app($body, $out_trade_no, $total_fee, $notify_url)
    {
        $info = (new \app\common\model\Config())->column('val', 'key');
        $config = [
            'app_id' => $info['wechat_appid'],
            'secret' => $info['wechat_secret'],
            'mch_id' => $info['wechat_mchid'],
            'key' => $info['wechat_key'],
        ];
        $payment = Factory::payment($config);
        $result = $payment->order->unify([
            'body' =>  $body,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $total_fee * 100,
            'notify_url' => config('app.app_host') . $notify_url,
            // 请对应换成你的支付方式对应的值类型 JSAPI - 公众号内支付  NATIVE - 扫码支付  MWEB - H5支付 APP - app支付
            'trade_type' => 'APP'
        ]);
        if ($result['return_code'] == "FAIL") return ['state' => 0, 'body' => $result['return_msg']];
        $jssdk = $payment->jssdk;
        return ['state' => 1, 'body' => $jssdk->appConfig($result['prepay_id'])];
    }

    /**
     * 小程序支付
     *
     * @param [type] $body  商品描述
     * @param [type] $out_trade_no  订单编号
     * @param [type] $total_fee 订单金额
     * @param [type] $notify_url    回调地址
     * @param [type] $code  小程序传过来的code  用户获取用户openid
     * @return void
     */
    public function applet($body, $out_trade_no, $total_fee, $notify_url, $code = "")
    {
        if (empty($code)) return ['state' => 0, 'body' => '没有获取到code'];
        $info = (new \app\common\model\Config())->column('val', 'key');
        $config = [
            'app_id' => $info['applet_appid'],
            'secret' => $info['applet_secret'],
            'mch_id' => $info['wechat_mchid'],
            'key' => $info['wechat_key'],
        ];
        $app = Factory::miniProgram($config);
        //通过小程序传过来的code 获取用户openid 如果绑定了微信开放平台同时能获取到 unionid
        $userInfo = $app->auth->session($code);
        $payment = Factory::payment($config);
        $result = $payment->order->unify([
            'body' =>  $body,
            'out_trade_no' => $out_trade_no,
            'total_fee' => $total_fee * 100,
            'notify_url' => config('app.app_host') . $notify_url,
            // 请对应换成你的支付方式对应的值类型 JSAPI - 公众号内支付  NATIVE - 扫码支付  MWEB - H5支付 APP - app支付
            'trade_type' => 'JSAPI',
            'openid' => $userInfo['openid'],
        ]);
        if ($result['return_code'] == "FAIL") return ['state' => 0, 'body' => $result['return_msg']];
        $jssdk = $payment->jssdk;
        return ['state' => 1, 'body' => $jssdk->bridgeConfig($result['prepay_id'], false)];
    }

    /**
     * 小程序支付回调
     */
    public function notify_applet()
    {
        $info = (new \app\common\model\Config())->column('val', 'key');
        $config = [
            'app_id' => $info['applet_appid'],
            'secret' => $info['applet_secret'],
            'mch_id' => $info['wechat_mchid'],
            'key' => $info['wechat_key'],
        ];
        return Factory::payment($config);
    }

    /**
     * app支付回调
     */
    public function notify_app()
    {
        $info = (new \app\common\model\Config())->column('val', 'key');
        $config = [
            'app_id' => $info['wechat_appid'],
            'secret' => $info['wechat_secret'],
            'mch_id' => $info['wechat_mchid'],
            'key' => $info['wechat_key'],
        ];
        return Factory::payment($config);
    }


    /**
     * 退款
     *
     * @param  $out_trade_no  商户订单号
     * @param  $out_request_no 商户退款单号
     * @param  $total_fee 订单金额
     * @param  $money 退款金额
     * @param  $refund_desc 退款说明
     * @return void
     */
    public function refund($out_trade_no, $out_request_no, $total_fee, $money, $refund_desc)
    {
        $info = (new \app\common\model\Config())->column('val', 'key');
        $config = [
            'app_id' => $info['wechat_appid'],
            'secret' => $info['wechat_secret'],
            'mch_id' => $info['wechat_mchid'],
            'key' => $info['wechat_key'],
            'cert_path'  => Env::get('root_path') . 'public/' . $info['wechat_cert_path'],
            'key_path'   => Env::get('root_path') . 'public/' . $info['wechat_key_path']
        ];
        $app = Factory::payment($config);
        $result = $app->refund->byOutTradeNumber(
            $out_trade_no,
            $out_request_no,
            $total_fee * 100,
            $money * 100,
            [
                // 可在此处传入其他参数，详细参数见微信支付文档
                'refund_desc' => $refund_desc,
            ]
        );
        if ($result['result_code'] == 'SUCCESS') {
            return ['state' => 1, 'body' => '退款成功'];
        } else {
            return ['state' => 0, 'body' => $result['err_code_des']];
        }
    }
}
